<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: LJQ <liujiqi@etrol.com> 
// +----------------------------------------------------------------------

namespace app\common\controller;
use app\admin\library\Auth;


class Admin  extends Base {

 /**
     * 无需登录的方法,同时也就不需要鉴权了
     * @var array
     */
    protected $noNeedLogin = [];
	
    /**
     * 无需鉴权的方法,但需要登录
     * @var array
     */
    protected $noNeedRight = [];
    
    protected $auth;
    
    //数据权限范围
    protected $scope;
    
    
    
    public function _initialize()
    {
    	parent::_initialize();
    
    //start权限验证部分--------------------------------------------------------------------
		$this->auth = Auth::instance();		
		// 检测是否需要验证登录
		if (!$this->auth->match($this->noNeedLogin))
		{
			//检测是否登录
			if (!$this->auth->isLogin())
			{				
				$this->error('你还没有登录', url('index/index/login'));
			}
			// 判断是否需要验证权限
			if (!$this->auth->match($this->noNeedRight))
			{

				// 判断控制器和方法判断是否有对应权限
				if (!$this->auth->check($this->url))
				{
					$this->error('你没有权限进入该页面',null);


				}
			}
			
			/* 读取数据库中的配置 */
			$config = cache('db_config_data');
			if (!$config) {
				$config = model('Config')->lists();
				cache('db_config_data', $config,3600*24);//缓存保存1天
			}
			config($config);
			//数据权限
			if(session('user_auth.userProperty') == 2){
				$this->scope = session('user_auth.enterpriseId');
			}
			//菜单数据
			$menu = $this->auth->getMenu($this->url);
			$breadCrumb = $this->auth->getBreadCrumb($this->url);
			//审核事件数量
			if($this->scope){
				$wfitem = model('Wfitem')->where(array('status'=>0))->where('enterpriseId',$this->scope)->count();
			}else{
				$wfitem = model('Wfitem')->where(array('status'=>0))->count();
			}
			$this->assign('wfitem',$wfitem);
			
			//告警事件数量
			$event = model('duty/events')->where(array('eventStatus'=>1))->count();
			$this->assign('event',$event);
			//报警数量
			if($this->scope){
				$alarm = model('alarm/alarm')->hasWhere('dangerSource',['enterpriseId'=>$this->scope])->where('alarm.status','in','0,1')->count();
			}else{
				$alarm = model('alarm/alarm')->where('status','in','0,1')->count();
			}
			
			$this->assign('alarm',$alarm);

			
//			$this->assign(('warn',$warn));
			$this->assign('breadCrumb',$breadCrumb);
			$this->assign('user',session('user_auth'));
			$this->assign('menu',$menu);
		}
    	
    }

}

